// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/testing/gtest_utils.h>
#include <pollux/testing/functions/prestosql/function_base_test.h>

using namespace kumo::pollux;
using namespace kumo::pollux::test;

class CoalesceTest : public functions::test::FunctionBaseTest {};

TEST_F(CoalesceTest, basic) {
  vector_size_t size = 20;

  // 0, null, null, 3, null, null, 6, null, null...
  auto first = make_flat_vector<int32_t>(
      size,
      [](vector_size_t row) { return row; },
      [](vector_size_t row) { return row % 3 != 0; });

  // 0, 10, null, 30, 40, null, 60, 70...
  auto second = make_flat_vector<int32_t>(
      size, [](vector_size_t row) { return row * 10; }, null_every(3, 2));

  // 0, 100, 200, 300, 400, 500, 600...
  auto third = make_flat_vector<int32_t>(
      size, [](vector_size_t row) { return row * 100; });

  auto row = make_row_vector({first, second, third});
  auto result = evaluate<FlatVector<int32_t>>("coalesce(c0, c1, c2)", row);
  auto expectedResult = make_flat_vector<int32_t>(
      size, [](auto row) { return row * pow(10, row % 3); });
  assertEqualVectors(expectedResult, result);

  // Verify that input expressions are evaluated only on rows that are still
  // null after evaluating all the preceding inputs and not evaluated at all if
  // there are no nulls remaining.

  // The last expression 'c1 / 0' should not be evaluated.
  result = evaluate<FlatVector<int32_t>>(
      "coalesce(c0, c1, c2, cast(c1 / 0 as integer))", row);
  assertEqualVectors(expectedResult, result);

  // The second expression 'c1 / (c1 % 3)' should not be evaluated on rows where
  // c1 % 3 is zero.
  result = evaluate<FlatVector<int32_t>>(
      "coalesce(c0, cast(c1 / (c1 % 3) as integer), c2)", row);
  assertEqualVectors(expectedResult, result);

  result = evaluate<FlatVector<int32_t>>("coalesce(c2, c1, c0)", row);
  expectedResult =
      make_flat_vector<int32_t>(size, [](auto row) { return row * 100; });
  assertEqualVectors(expectedResult, result);

  result = evaluate<FlatVector<int32_t>>("coalesce(c0, c1)", row);
  expectedResult = make_flat_vector<int32_t>(
      size,
      [](auto row) { return row * pow(10, row % 3); },
      [](auto row) { return row % 3 == 2; });
  assertEqualVectors(expectedResult, result);
}

TEST_F(CoalesceTest, strings) {
  auto input = make_row_vector({
      make_nullable_flat_vector<StringView>(
          {"a", std::nullopt, std::nullopt, "d", std::nullopt}),
      make_nullable_flat_vector<StringView>(
          {"aa", std::nullopt, std::nullopt, "dd", std::nullopt}),
      make_nullable_flat_vector<StringView>(
          {"aaa", "bbb", std::nullopt, "ddd", "eee"}),
  });

  auto expectedResult = make_nullable_flat_vector<StringView>(
      {"a", "bbb", std::nullopt, "d", "eee"});

  auto result = evaluate<FlatVector<StringView>>("coalesce(c0, c1, c2)", input);
  assertEqualVectors(expectedResult, result);
}

TEST_F(CoalesceTest, incompatibleTypes) {
  auto input = make_row_vector({
      make_nullable_flat_vector<StringView>(
          {"a", std::nullopt, std::nullopt, "d", std::nullopt}),
      make_nullable_flat_vector<int32_t>({1, 2, 3, 4, 5}),
  });

  POLLUX_ASSERT_THROW(
      evaluate<FlatVector<StringView>>("coalesce(c0, c1)", input),
      "Inputs to coalesce must have the same type. Expected VARCHAR, but got INTEGER.");
}
